/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ar.uba.fi.posgrado.economy.web.action.account;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ar.uba.fi.posgrado.economy.model.Customer;
import ar.uba.fi.posgrado.economy.model.Debt;
import ar.uba.fi.posgrado.economy.service.CustomerService;
import ar.uba.fi.posgrado.economy.service.DebtService;
import ar.uba.fi.posgrado.economy.web.form.customer.CustomerAccountForm;

/**
 * 
 * @author pablo.tortorella
 */
public class ShowCustomerAccountDetailsAction extends Action {

	private DebtService debtService;

	private CustomerService customerService;

	/**
	 * @param customerService
	 *            the customerService to set
	 */
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CustomerAccountForm form = (CustomerAccountForm) actionForm;
		Long accountId = form.getAccountId();
		Long customerId = form.getCustomerId();

		// busco al cliente y su cuenta
		Customer customer = customerService.getById(customerId);

		Debt debt = new Debt();
		debt.setCustomer(customer);
		// busco los pagos realizados al docente
		List<Debt> customerDebts = debtService.getDebtsByConditions(debt);

		form.setAccountId(accountId);
		form.setCustomerId(customerId);
		form.setCustomerName(customer.getName());
		form.setDebts(customerDebts);

		return mapping.findForward("success");
	}

	/**
	 * @param debtService
	 *            the debtService to set
	 */
	public void setDebtService(DebtService debtService) {
		this.debtService = debtService;
	}

}
